const Modal = require("../modal/user")
class User {
  constructor() {
    this.instance = null
  }
  static get Instance() {
    return this.instance || (this.instance = new User())
  }

  async getUsers(pageNum, skipNum, q) {
    const users = await Modal.find({
      username: new RegExp(decodeURIComponent(q))
    })
      .limit(pageNum)
      .skip(skipNum)
    return users
  }

  async updateUser(uid, data) {
    // let user = await Modal.findOne({ _id: uid }).exec()
    // user = await user.updateOne(data)
    const user = await Modal.findByIdAndUpdate(uid, data)
    if (!user) return null
    return user
  }

  async addUser(data) {
    const username = data.username
    const _user = await Modal.findOne({ username }).exec()
    if (_user) return null
    const user = new Modal(data)
    await user.save()
    return user
  }

  async getUserById(uid, fields) {
    let querySelector = ""
    let populateStr = ""
    if (fields) {
      const fieldsList = fields.split(";").filter(v => v)
      querySelector = fieldsList.map(v => " +" + v).join("")
      populateStr = fieldsList
        .map(v => {
          if (v === "employments") return "employments.company employments.job"
          else if (v === "educations")
            return "educations.school educations.major"
          return v
        })
        .join(" ")
    }
    // const res = Modal.findById(uid)
    const user = await Modal.findById(uid).select(querySelector).populate(populateStr).exec()
    // const user = await (!querySelector
    //   ? res.exec()
    //   : res.select(querySelector).exec())
    if (!user) return null
    return user
  }

  async delUserById(uid) {
    // const user = await this.findOne({ _id: uid }).exec()
    // if (!user) return null
    // await user.remove()
    // return user
    const user = await Modal.findByIdAndRemove(uid)
    if (!user) return null
    return user
  }

  async getUserByModal(data) {
    const user = await Modal.findOne(data).exec()
    return user
  }

  async listFollowings(uid) {
    const user = await Modal.findById(uid)
      .select("+followings")
      .populate("followings")
    if (!user) return null
    return user
  }

  async addFollowing(uid, fid) {
    // 这里的uid是通过token获取，用户必须已登陆， 不存在当前用户不存在的情况
    const follow_user = await Modal.findById(fid).exec()
    if (!follow_user) return null
    const me = await Modal.findById(uid).select("+followings")
    if (me.followings.map(follow_id => follow_id.toString()).includes(fid))
      return null
    me.followings.push(fid)
    me.save()
    return me
  }
  async cancelFollowing(uid, fid) {
    const follow_user = await Modal.findById(fid).exec()
    if (!follow_user) return null
    const me = await Modal.findById(uid).select("+followings")
    const index = me.followings
      .map(follow_id => follow_id.toString())
      .indexOf(fid)
    if (index < 0) return null
    me.followings.splice(index, 1)
    me.save()
    return me
  }

  async listFans(uid) {
    const users = Modal.find({ followings: uid }).exec()
    return users
  }
}

const I = User.Instance
module.exports = I
